import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SystemApi } from '#/api/system/system';

import { useAccess } from '@vben/access';

const { hasAccessByCodes } = useAccess();

export function useCompanyFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '公司名称',
      component: 'Input',
      rules: 'required',
    },
  ];
}

export function useCompanyGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '公司名称',
      component: 'Input',
    },
  ];
}

/** 列表的字段 */
export function useCompanyGridColumns<T = SystemApi.System>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'seq',
      title: '序号',
      width: 50,
    },
    {
      field: 'expand',
      type: 'expand',
      width: 50,
      align: 'center',
      slots: { content: 'expand-content' },
    },
    {
      field: 'name',
      title: '公司名称',
      minWidth: 100,
    },
    {
      field: 'editInfo',
      title: '编辑信息',
      slots: { default: 'edit-info' },
      minWidth: 200,
    },
    {
      field: 'operation',
      title: '操作',
      width: 240,
      fixed: 'right',
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'company',
          nameTitle: '公司',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['system:company:edit']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['system:company:delete']),
          },
          {
            code: 'add-department',
            text: '添加部门',
            show: hasAccessByCodes(['system:department:add']),
          },
        ],
      },
    },
  ];
}

export function useDepartmentFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'companyId',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '部门名称',
      component: 'Input',
      rules: 'required',
    },
  ];
}

export function useDepartmentGridColumns<T = SystemApi.System>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'seq',
      title: '序号',
      width: 50,
    },
    {
      field: 'name',
      title: '部门名称',
      minWidth: 100,
    },
    {
      field: 'editInfo',
      title: '编辑信息',
      slots: { default: 'edit-info' },
      minWidth: 200,
    },
    {
      field: 'operation',
      title: '操作',
      width: 240,
      fixed: 'right',
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'department',
          nameTitle: '部门',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['system:department:edit']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['system:department:delete']),
          },
        ],
      },
    },
  ];
}
